package com.husd.framework.nio;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;

/**
 * @author hushengdong@gome.com.cn
 * @date 2022/1/23 12:00 下午
 */
public class ChannelTransferExample {

    // channel 之间复制数据
    public static void main(String[] args) throws IOException {

        RandomAccessFile fromFile = new RandomAccessFile("/tmp/fromFile.txt", "rw");
        FileChannel      fromChannel = fromFile.getChannel();

        RandomAccessFile toFile = new RandomAccessFile("/tmp/toFile.txt", "rw");
        FileChannel toChannel = toFile.getChannel();

        long position = 0;
        long count  = fromChannel.size();

        // 这2个方法基本是一样的。
        //toChannel.transferFrom(fromChannel, position, count);
        fromChannel.transferTo(position, count, toChannel);

        fromChannel.close();
        toChannel.close();
    }
}
